// components/shopLIstItem/index.js
const api = require('../../utils/request.js')
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    goods: {
      type: Object,
      value: {
        count: 0
      }
    },
    goodsIndex: {
      type: Number,
      value: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgHost: api.IMG_HOST
  },

  /**
   * 组件的方法列表
   */
  methods: {
    subtractNums () {
      let goods = { ...this.data.goods}
      let count = goods.Number
      if ( count > 0 ) {
        count -= 1
        goods.Number = count
        this.updateCount(goods, this.data.goodsIndex)
      }
    },
    addNums() {
      let goods = { ...this.data.goods }
      let count = goods.Number
      count += 1
      goods.Number = count
      this.updateCount(goods, this.data.goodsIndex)
    },
    updateCount(goods, index) {
      return new Promise(resolve => {
        api._put('/User/Design/Goods/Update', {
          CartDetailsID: goods.GoodsInCartID,
          Number: goods.Number
        }).then(res => {
          if (res.Code === 100000) {
            this.triggerEvent('countChange', {
              index: index,
              goods: goods
            })
            resolve()
          }
        })
      })
    }
  }
})
